﻿using System.Text;
using System.IO;

public static class MD5Helper {

    public static string ComputeMD5(byte[] content)
    {
        System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
        byte[] md5Value = md5.ComputeHash(content);
        return MD5ByteToStr(md5Value);
    }

    public static string ComputeMD5(Stream content)
    {
        System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
        byte[] md5Value = md5.ComputeHash(content);
        return MD5ByteToStr(md5Value);
    }

    private static string MD5ByteToStr(byte[] md5Value)
    {
        
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < md5Value.Length; j++)
        {
            sb.Append(md5Value[j].ToString("X2"));
        }
        return sb.ToString();
    }
}
